﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using IR.WPF.Interfaces;
using IR.WPF.ViewModels.LandingViewModels;

namespace IR.WPF.Windows
{
    /// <summary>
    /// Interaction logic for SingleControlHost.xaml
    /// </summary>
    public partial class SingleControlHost : Window
    {
        private bool _isModal;

        public SingleControlHost()
        {
            InitializeComponent();

            MainWindow tempMainWindow = App.Current.MainWindow as MainWindow;
            if (tempMainWindow.LoggedInUser == null)
                this.Close();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            IHandleCleanup cleanup = DataContext as IHandleCleanup;

            if (cleanup != null)
                cleanup.DoCleanup();

            base.OnClosing(e);
        }

        /// <summary>
        /// Displays a Modeless Window containing the control (of T)
        /// </summary>
        /// <typeparam name="T">Type of Control that will be instantiated and displayed</typeparam>
        /// <param name="viewModel"></param>
        /// <param name="owner"></param>
        public static void ShowWindow<T>(ViewModelBase viewModel, Window owner) where T : Control, new()
        {
            SingleControlHost window = new SingleControlHost();
            // Create the hosted control
            T hostedControl = new T();

            // Assign the control into the window
            window.Content = hostedControl;
            // Assign the view model to the data context
            window.DataContext = viewModel;
            window.Owner = owner;
            window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            viewModel.BoundWindow = window;
            if (viewModel.WindowTitle != null)
                window.Title = viewModel.WindowTitle;
            window.Show();
        }

        public static bool ShowModal<T>(ViewModelBase viewModel, Window owner) where T : Control, new()
        {
            SingleControlHost window = new SingleControlHost();
            // Create the hosted control
            T hostedControl = new T();

            // Assign the control into the window
            window.Content = hostedControl;
            // Assign the view model to the data context
            window.DataContext = viewModel;
            window.Owner = owner;
            window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            window._isModal = true;
            if (viewModel.WindowTitle != null)
                window.Title = viewModel.WindowTitle;
            viewModel.BoundWindow = window;

            // Check for valid result from the Dialog
            return window.ShowDialog() ?? false;
        }

        public bool IsModal
        {
            get { return _isModal; }
        }
    }
}
